import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
import numpy as np
from sklearn.preprocessing import StandardScaler
# 特征最影响结果的K个特征
from sklearn.feature_selection import SelectKBest

# 卡方检验，作为SelectKBest的参数
from sklearn.feature_selection import chi2

# 选择所有的特征，目的是看到特征重要性排序
# bestfeatures = SelectKBest(score_func=chi2, k=len(tezheng))
# fitt = bestfeatures.fit(df[tezheng], (df['up_945'] / 3))
# df_scores = pd.DataFrame(fitt.scores_)


### 需要多棵树
### 需要根据特征重要性程度， 去找相似的特征

def buy_flow(x_test, y_test, dec):
    i = 0
    money = 100000
    count = 0
    count_map = {}
    err_count_map = {}
    for res in dec.predict(x_test):
        if str(res) in count_map:
            count_map[str(res)] = count_map[str(res)] + 1
        else:
            count_map[str(res)] = 1
        if res > 1:
            print(res, "|||", y_test[i])
            if y_test[i] < 0:
                count = count + 1
                if str(res) in err_count_map:
                    err_count_map[str(res)].append(y_test[i])
                else:
                    err_count_map[str(res)] = [y_test[i]]
            money = money * (1 + y_test[i] * 2/100)
        i = i + 1
    print(count_map)
    print(err_count_map)
    print("--交易了----", count, ":", money)

def decision():
    df = pd.read_csv("E:\\ts_data\\moneyflow\\all01.csv")
    # print(df.head())
    df.dropna(inplace=True)
    # df = df[df['up_945'] > 3]
    # print(df.dtypes)
    # 特征值

    tezheng = [
               'act_buy_xl_10', 'act_buy_xl_20', 'act_buy_xl_40', 'act_buy_xl_80',
               'act_sell_xl_10', 'act_sell_xl_20', 'act_sell_xl_40', 'act_sell_xl_80',
               'act_buy_l_10', 'act_buy_l_20', 'act_buy_l_40', 'act_buy_l_80',
               'act_sell_l_10', 'act_sell_l_20', 'act_sell_l_40', 'act_sell_l_80',
               'act_buy_m_10', 'act_buy_m_20', 'act_buy_m_40', 'act_buy_m_80',
               'act_sell_m_10', 'act_sell_m_20', 'act_sell_m_40', 'act_sell_m_80',
               'dde_10', 'dde_20', 'dde_40', 'dde_80',
               'up_10m', 'up_20m', 'up_40m', 'up_80m',
               '1d_up', 'turnover', 'turnover_rate', '3d_up']
    # 'close_1d',
    print(df.columns)

    print(df[['dde_l', 'dde_10', 'dde_20', 'dde_40', 'dde_80']].head())
    print(df[['act_buy_xl_10', 'act_buy_xl_20', 'act_buy_xl_40', 'act_buy_xl_80']].head())
    print(df[['act_sell_xl_10', 'act_sell_xl_20', 'act_sell_xl_40', 'act_sell_xl_80']].head())
    print(df[['up_10m', 'up_20m', 'up_40m', 'up_80m']].head())
    x = df[tezheng].values
    # 目标值
    # y = pd.cut(df['up_945'], [-50, -10, -5, -2, 2, 5, 10, 50], labels=False)
    y = (df['up_940']/2).values
    # up_950 72.72%
    # up_945 75.47%
    # up_940 78.08%
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)

    print("X_train_shape:", x_train.shape, " y_train_shape:", y_train.shape)
    print("X_test_shape:", x_test.shape, "  y_test_shape:", y_test.shape)

    from collections import Counter

    print('Counter(data)\n', Counter(np.around(y_test)))
    # max_depth 5 或 6
    # 剪纸
    # 随机森林
    # dec = DecisionTreeClassifier(max_depth=6)
    # dec.fit(x_train, y_train.astype('int'))
    dec = XGBClassifier()
    dec.fit(x_train, np.around(y_train.astype('int')))
    # dec.save_model("stcok_xgb_1.model")
    # dec.load_model("stcok_xgb_1.model")
    # print("----", dec.score(x_train, y_train.astype('int')))
    print("----", dec.score(x_test, np.around(y_test).astype('int')))
    # print(dec.feature_importances_)

    print("--------------------------------------------------")
    y_pred = dec.predict(x_test)
    print("--------------------------------------------------")
    accuracy = accuracy_score(np.around(y_test.astype('int')), y_pred)
    print("--------------------------------------------------")
    print("accuarcy: %.2f%%" % (accuracy * 100.0))  # accuarcy: 87.84%
    buy_flow(x_test, y_test, dec)
    print("------------------end")



    # import matplotlib.pyplot as plt
    #
    # from xgboost import plot_importance
    #
    # fig, ax = plt.subplots(figsize=(10, 15))
    #
    # plot_importance(dec, height=0.5, max_num_features=64, ax=ax)
    #
    # plt.show()
    # print("end")


    # export_graphviz(dec, feature_names=tezheng, out_file="./tree.dot")
    # dot -Tpdf tree.dot -o tree.pdf
def load_decision():
    dec = XGBClassifier()
    dec.load_model("stcok_xgb.model")

if __name__ == "__main__":
    decision()

    # df = pd.read_csv("E:\\ts_data\\moneyflow\\all.csv")
    # std =StandardScaler()
    # data = std.fit_transform(df[['dde_l', 'dde_10', 'dde_20', 'dde_40', 'dde_80']].values)
    # print(data)
    # todo 进行标准化（）
